﻿using PacoToolKit.Format;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Protocol.SouthGridProtocol.Blocks
{
    public class FileTransferCheckBlock
    {
        /// <summary>
        /// 文件性质
        /// </summary>
        public string fileType
        {
            get;
            set;
        }
        /// <summary>
        /// 文件总段数
        /// </summary>
        public int fileGroupCount { get; set; }

        /// <summary>
        /// 文件大小
        /// </summary>
        public int fileSize { get; set; }

        /// <summary>
        /// 文件名称
        /// </summary>
        public string fileName { get; set; }

        /// <summary>
        /// 起始段号
        /// </summary>
        public int fileBeginIndex { get; set; }

        /// <summary>
        /// 结束段号
        /// </summary>
        public int fileEndIndex { get; set; }

        /// <summary>
        /// 上行返回的未成功接收的总段数 m
        /// </summary>
        public int errorCount { get; set; }

        /// <summary>
        /// 未成功接收的段号列表 2*m
        /// </summary>
        public List<int> errorIndexList { get; set; }

        /// <summary>
        /// 构造FileTransferInfoBlock
        /// </summary>
        /// <param name="fileType"></param>
        /// <param name="fileGroupCount"></param>
        /// <param name="fileSize"></param>
        /// <param name="fileName"></param>
        /// <param name="fileSuccessReceivedCount"></param>
        public FileTransferCheckBlock(string fileType, int fileGroupCount, int fileSize, string fileName, int fileBeginIndex, int fileEndIndex)
        {
            this.fileType = fileType;
            this.fileGroupCount = fileGroupCount;
            this.fileSize = fileSize;
            this.fileName = fileName;
            this.fileBeginIndex = fileBeginIndex;
            this.fileEndIndex = fileEndIndex;
        }

        /// <summary>
        /// 构造FileTransferInfoBlock
        /// </summary>
        /// <param name="fileType"></param>
        /// <param name="fileGroupCount"></param>
        /// <param name="fileSize"></param>
        /// <param name="fileName"></param>
        /// <param name="fileSuccessReceivedCount"></param>
        public FileTransferCheckBlock(string fileType, int fileGroupCount, int fileSize, string fileName, int fileBeginIndex, int fileEndIndex,int errorCount)
        {
            this.fileType = fileType;
            this.fileGroupCount = fileGroupCount;
            this.fileSize = fileSize;
            this.fileName = fileName;
            this.fileBeginIndex = fileBeginIndex;
            this.fileEndIndex = fileEndIndex;
            this.errorCount = errorCount;
        }

        /// <summary>
        /// 数据块转换成报文
        /// </summary>
        /// <returns></returns>
        public string toString()
        {
            string blocks = Formater.ReverseHexString(fileType) +
                Formater.ReverseHexString(Formater.DECtoHEX(fileGroupCount.ToString(), 4)) +
                Formater.ReverseHexString(Formater.DECtoHEX(fileSize.ToString(), 8)) +
                Formater.AdjustLengthBottom(Formater.StringToASCLLHexStr(fileName), 64);//ascii no reverse
            return Formater.BytesToHexStr(Formater.crc16(Formater.HexStringToBytesKeep(blocks))) +
                Formater.ReverseHexString(Formater.DECtoHEX(fileBeginIndex.ToString(), 4)) +
                Formater.ReverseHexString(Formater.DECtoHEX(fileEndIndex.ToString(), 4));
        }
    }
}
